export default {
  findDeviceByValue(devices, targetValue) {
    for (const device of devices) {
      if (device.DevicetypeValue === targetValue) {
        return device // 如果找到值等于 "2001" 的设备，直接返回整个设备对象
      }

      if (device.children && device.children.length > 0) {
        // 使用递归进行深度搜索
        const result = this.findDeviceByValue(device.children, targetValue)
        if (result) {
          return result // 如果在子树中找到匹配的设备，返回结果
        }
      }
    }
    return null // 如果没有找到值等于 "2001" 的设备，返回 null
  },

  SearchAuto(devices) {
    const groupedDevices = devices.reduce((acc, device) => {
      const deviceId = device.deviceId
      if (!acc[deviceId]) {
        acc[deviceId] = []
      }
      acc[deviceId].push(device)
      return acc
    }, {})

    // Convert the grouped devices object into an array
    const resultArrays = Object.values(groupedDevices)

    return resultArrays
  },

  groupDevicesById(devices) {
    const groupedDevices = devices.reduce((acc, device) => {
      const deviceId = device.id; // 使用设备的 id 属性进行分组
      if (!acc[deviceId]) {
        acc[deviceId] = [];
      }
      acc[deviceId].push(device);
      return acc;
    }, {});

    // 将分组后的设备对象转换为数组
    const resultArrays = Object.values(groupedDevices);
    return resultArrays;
  },


  // 搜索
  seachList(seachchange, page, pagesize) {
    let res
    let Searchlist = []
    let totalList = {
      currentPage: null,
      pageSize: null,
      total: null,
      totalPage: null
    }
    res = window.api.pageDvcAndDvcVer(JSON.stringify(seachchange), page, pagesize)

    // 当前页
    totalList.currentPage = res?.getPageSync()
    // 每页数量
    totalList.pageSize = res?.getPageSizeSync()
    // 总页数
    totalList.totalPage = res?.getTotalPageSync()
    // 总条数
    totalList.total = res?.getTotalSync()

    const size = res?.sizeSync()

    for (let i = 0; i < size; i++) {
      const result = res?.getSync(i)
      const id = result.getSync('id')
      const pid = result.getSync('pid')
      const name = result.getSync('name')
      const channels = result.getSync('channels')
      const DevicetypeName = result.getSync('DevicetypeName')
      const DevicetypeValue = result.getSync('DevicetypeValue')
      const isShow = result.getSync('isShow')
      const amplitude = result.getSync('amplitude')
      const delay = result.getSync('delay')
      const isVirtual = result.getSync('isVirtual')
      const ComPort = result.getSync('ComPort') ? result.getSync('ComPort') : undefined
      const BaudRate = result.getSync('BaudRate') ? result.getSync('BaudRate') : undefined
      const isDelete = result.getSync('isDelete')
      const property = result.getSync('property')
      const values = result.getSync('values')
      const sensorType = result.getSync('sensorType')
      const DeviceVariable = result.getSync('deviceVariable')

      let list = {
        id: id,
        pid: pid,
        name: name,
        DevicetypeName: DevicetypeName,
        DevicetypeValue: DevicetypeValue,
        Channels: channels,
        isShow: isShow,
        amplitude: amplitude,
        delay: delay,
        isVirtual: isVirtual,
        ComPort: ComPort,
        BaudRate: BaudRate,
        isDelete: isDelete,
        property: property,
        values: values,
        sensorType: sensorType,
        DeviceVariable: DeviceVariable
      }
      Searchlist.push(list)
    }
    return { totalList, Searchlist }
  }
}
